package com.common.job;

import java.text.ParseException;

import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class RescheduleJob {
	Logger log = Logger.getLogger(RescheduleJob.class);
	private static SchedulerFactory sf = new StdSchedulerFactory();   
	    
    private static String JOB_GROUP_NAME = "group";   
    private static String TRIGGER_GROUP_NAME = "trigger";   
    private static String str = "trigger";   
	public static int ei=4;
	public RescheduleJob() {
		super();
		/*try {
			startJob("testjob",new TestJob(),"0/20 * * * * ?");
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
	}

   
    public void startJob(String jobName, Class jobClass, String time)   
            throws SchedulerException, ParseException { 
    	log.info("startJob==jobName==="+jobName);
        Scheduler sched = sf.getScheduler();   
  
        JobDetail jobDetail = new JobDetail();   
        jobDetail.setName(jobName);   
        jobDetail.setGroup(JOB_GROUP_NAME);   
        jobDetail.setJobClass(jobClass);   
  
        CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);   
        trigger.setCronExpression(time);  
        
        sched.scheduleJob(jobDetail, trigger);   
  
        if (!sched.isShutdown()) {   
            sched.start();   
        }   
        str="endstart=====";
    }   
  
    public void modifyJobTime(String triggerName, String time)   
            throws SchedulerException, ParseException {   
    	log.info("startJob====="+str);
    	log.info("======modifyJobTime==start======");
        Scheduler sched = sf.getScheduler();   
        
        CronTrigger trigger = (CronTrigger)sched.getTrigger(triggerName, TRIGGER_GROUP_NAME);
        trigger.setCronExpression(time);     
      
        sched.rescheduleJob(triggerName, TRIGGER_GROUP_NAME,trigger);
        str="endmodifyJobTime=====";
    }  
    public void modifyJobTime1(String jobName, String time,Class jobClass)   
    throws SchedulerException, ParseException {   
    	log.info("startJob====="+str);
    	Scheduler sched = sf.getScheduler();
    	 CronTrigger trigger = (CronTrigger)sched.getTrigger(jobName, TRIGGER_GROUP_NAME);
		if (trigger != null) {
			CronTrigger ct = (CronTrigger) trigger;
			// 移除当前进程的Job
			sched.deleteJob(jobName, JOB_GROUP_NAME);
			// 修改Trigger
			ct.setCronExpression(time);
			log.info("====modifyJobTime1===== " + ct.getJobName());
			// 重新调度jobDetail
			 JobDetail jobDetail = new JobDetail();   
		        jobDetail.setName(jobName);   
		        jobDetail.setGroup(JOB_GROUP_NAME);   
		        jobDetail.setJobClass(jobClass);
		        
			sched.scheduleJob(jobDetail, ct);
		}
		 str="modifyJobTime1=====";
    }  
    public void modifyJobTime2(String jobName, String time,Class jobClass)   
    throws SchedulerException, ParseException {   
    	Scheduler sched = sf.getScheduler();
    	CronTrigger trigger = (CronTrigger)sched.getTrigger(jobName, TRIGGER_GROUP_NAME);
    	if (trigger != null) {
    		CronTrigger ct = (CronTrigger) trigger;
    		// 移除当前进程的Job
    		sched.deleteJob(jobName, JOB_GROUP_NAME);
    		// 修改Trigger
    		ct.setCronExpression(time);
    		log.info("=====modifyJobTime2====== " + ct.getJobName());
    		// 重新调度jobDetail
    		JobDetail jobDetail = new JobDetail();   
    		jobDetail.setName(jobName);   
    		jobDetail.setGroup(JOB_GROUP_NAME);   
    		jobDetail.setJobClass(jobClass);
    		sched.shutdown();
    		sched = sf.getScheduler();
    		sched.scheduleJob(jobDetail, ct);
    		sched.start();
    	}
    	
    }  
}
